package com.example.fiction.ui.fragment.books;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.fiction.BR;
import com.example.fiction.R;
import com.example.fiction.base.BaseRecyclerViewAdapter;
import com.example.fiction.base.BaseResult;
import com.example.fiction.databinding.FragmentBooksBinding;
import com.example.fiction.db.BooksDBHelper;
import com.example.fiction.interfacz.IDirectory;
import com.example.fiction.response.BooksResponse;
import com.example.fiction.service.BooksLoader;
import com.example.fiction.ui.activity.reader.ReaderActivity;
import com.example.fiction.utils.DisplayUtil;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * 书架
 * auth: 姚鸿杰
 * time: 2019/3/19 22:09
 **/
public class BooksFragment extends BaseFragment<FragmentBooksBinding, BooksViewModel> {

    private final String TAG = "BooksFragment";

    @Override
    public int initContentView(LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
        return R.layout.fragment_books;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        ((AppCompatActivity) getActivity()).setSupportActionBar(binding.booksTbr);
        // 取消标题
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
        setHasOptionsMenu(true);

        viewModel.addRV(binding.booksRv, getActivity());
        viewModel.getBooksInfo();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.books, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:
                Toast.makeText(getActivity(), "搜索", Toast.LENGTH_SHORT).show();
                break;
            case R.id.changeLayout:
//                Toast.makeText(getActivity(), "更改布局", Toast.LENGTH_SHORT).show();
                viewModel.changeLayout(getActivity(), false);
                break;
            case R.id.addBooks:
                Toast.makeText(getActivity(), "添加书籍", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
